const jwt = require('jsonwebtoken');

const whiteList = [
  '/api/user/info',
  '/api/car',
  '/api/car/:id',
];
module.exports = () => {
  return async function gzip(ctx, next) {
    const token = ctx.request.header.token;
    const url = ctx.request.path;
    if (whiteList.includes(url)) {
      if (token) {
        try {
          ctx.info = jwt.verify(token, 'liuyu123456');
          await next();
        } catch (e) {
          console.log(e);
          ctx.throw(401, {
            code: 1,
            message: 'token失效',
          });
        }
      } else {
        ctx.throw(401, {
          code: 1,
          message: '用户未登录',
        });
      }
    } else {
      await next();
    }
  };
};
